package org.jeecg.modules.engine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.engine.entity.Engineering;
import org.jeecg.modules.engine.entity.Frame;
import org.jeecg.modules.engine.entity.Requirement;
import org.jeecg.modules.engine.mapper.FrameMapper;
import org.jeecg.modules.engine.mapper.RequirementMapper;
import org.jeecg.modules.engine.mapper.EngineeringMapper;
import org.jeecg.modules.engine.service.IEngineeringService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工程表
 * @Author: jeecg-boot
 * @Date:   2020-07-20
 * @Version: V1.0
 */
@Service
public class EngineeringServiceImpl extends ServiceImpl<EngineeringMapper, Engineering> implements IEngineeringService {

	@Autowired
	private EngineeringMapper engineeringMapper;
	@Autowired
	private FrameMapper frameMapper;
	@Autowired
	private RequirementMapper requirementMapper;

	@Override
	@Transactional
	public void delMain(String id) {
		if (frameMapper.selectCount(new LambdaQueryWrapper<Frame>().eq(Frame::getEngineeringId,id))>0){
			throw new RuntimeException("该生产单已经有删除构件了，不能删除");
		}
		if (requirementMapper.selectCount(new LambdaQueryWrapper<Requirement>().eq(Requirement::getEngineeringId,id))>0){
			throw new RuntimeException("该生产单已经有物料信息了，不能删除");
		}
		frameMapper.deleteByMainId(id);
		requirementMapper.deleteByMainId(id);
		engineeringMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			this.delMain(id.toString());
		}
	}

	@Override
	public IPage<Engineering> getEnginByProc(Page<Engineering> page, String procId,String engineName) {
		return baseMapper.getEnginByProc(page,procId,engineName);
	}

}
